package com.cqie.tes.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqie.tes.comm.R;
import com.cqie.tes.comm.qo.BaseQo;
import com.cqie.tes.comm.service.impl.BaseServiceImpl;
import com.cqie.tes.domain.Employee;
import com.cqie.tes.domain.SysUser;
import com.cqie.tes.domain.qo.tes.EmployeQo;
import com.cqie.tes.domain.vo.tes.employe.EmployeOptionVo;
import com.cqie.tes.domain.vo.tes.employe.EmployeVo;
import com.cqie.tes.mapper.EmployeeMapper;
import com.cqie.tes.mapper.SysUserMapper;
import com.cqie.tes.service.DeptService;
import com.cqie.tes.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fall in love
 * @since 2021-10-15
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DeptService deptService;
    // TODO 改为新项目结构
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public R getList(BaseQo query) {
        // 1
        EmployeQo qo = (EmployeQo) query;
//        System.out.println(query.getLimit());
        // 2
        QueryWrapper<Employee> qw = new QueryWrapper<>();
        if (!StringUtils.isEmpty(qo.getName())){
            qw.like("name",qo.getName());
        }
        qw.eq("mark", 1);
        qw.orderByDesc("id");
        // 3
        IPage<Employee> page = new Page<>(qo.getPage(),qo.getLimit());
        IPage<Employee> data = employeeMapper.selectPage(page,qw);
        List<Employee> entityList = data.getRecords();

        // 4
        List<EmployeVo> voList = new ArrayList<>();

        if (!entityList.isEmpty()) {
            entityList.forEach(item->{
                EmployeVo vo = new EmployeVo();
                BeanUtils.copyProperties(item,vo);
                // 设置vo字段
                // 部门名称
                vo.setDeptName(deptService.getById(vo.getDeptId()).getName());
                // 用户账号

                if (item.getUserId() != null){
                    SysUser sysUser = sysUserMapper.selectById(item.getUserId());
                    vo.setUserAccount(sysUser!=null?sysUser.getUsername():null);
                }

                voList.add(vo);
            });
        }
        Long total = data.getTotal();

        return R.ok().message("查询成功").data("total", total).data("rows", voList);
    }


    @Override
    public R edit(Employee entity) {
        return super.edit(entity);
    }

    @Override
    public R deleteById(Integer id) {
        if (id == null || id == 0) {
            return R.error().message("记录ID不能为空");
        }
        Employee entity = this.getById(id);
        if (entity == null) {
            return R.error().message("记录不存在");
        }
        return super.delete(entity);
    }

    @Override
    public R optionList() {
        // 结果列表
        List<EmployeOptionVo> vos  =new ArrayList<>();

        QueryWrapper<Employee> qw = new QueryWrapper<>();
        qw.eq("mark", 1);
        qw.orderByAsc("id");

        List<Employee> entityList = employeeMapper.selectList(qw);
        entityList.forEach(entity->{
            EmployeOptionVo vo = new EmployeOptionVo();
            BeanUtils.copyProperties(entity,vo);
            vos.add(vo);
        });
        return R.ok().message("查询成功").data("options", vos);
    }

}
